import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Index from '@/components/Index'
import UserInfo from '@/components/Users/UserInfo'
import AddUser from '@/components/Users/AddUser'
import UpPwd from '@/components/UpPwd'
import CompanyInfo from '@/components/Users/CompanyInfo'
import AddFixing from '@/components/GenreFix/AddFixing'
import TagInfo from '@/components/GenreFix/TagInfo'
import AddTag from '@/components/GenreFix/AddTag'
import FixingInfo from '@/components/GenreFix/FixingInfo'
import NetInfo from '@/components/GenreFix/NetInfo'
import AddNet from '@/components/GenreFix/AddNet'
import Report from '@/components/GenreFix/Report'
import OrderInfo from '@/components/Order/OrderInfo'
import OrderList from '@/components/Order/OrderList'
import AddOrder from '@/components/Order/AddOrder'
import CustomerInfo from '@/components/Order/CustomerInfo'
import AddCustomer from '@/components/Order/AddCustomer'
import PandectTotal from '@/components/Pandect/PandectTotal'
import Monitoring from '@/components/Pandect/Monitoring'
import SendGoods from '@/components/SendTakeGoods/SendGoods'
import TakeGoods from '@/components/SendTakeGoods/TakeGoods'
import RouterAn from '@/components/Analyze/RouterAn'
import TemperLog from '@/components/Temper/TemperLog'
import OrderContent from '@/components/Analyze/OrderContent'

Vue.use(Router)

const router = new Router({
	routes: [
		{			// 密码找回页面
			path: '/UpPwd',
			name: 'UpPwd',
			component: UpPwd
		},
		{			// 首页
			path: '/Index',
			name: 'Index',
			component: Index,
			redirect: '/pandect_total',
			children: [
				{ path: '/user_info', component: UserInfo },	// 用户信息页面
				{ path: '/add_user', component: AddUser },		// 添加用户页面
				{ path: '/comp_info', component: CompanyInfo },		// 公司信息页面
				{ path: '/add_fix', component: AddFixing },		// 添加货物类型
				{ path: '/fix_info', component: FixingInfo },		// 获取货物类型信息
				{ path: '/add_tag', component: AddTag },		// 添加标签
				{ path: '/tag_info', component: TagInfo },		// 获取标签信息
				{ path: '/net_info', component: NetInfo },		// 网关信息
				{ path: '/add_net', component: AddNet },		// 添加网关
				{ path: '/report', component: Report },		// 检查校准报告
				{ path: '/order_info', component: OrderInfo },		// 运单信息
				{ path: '/order_list', component: OrderList },		// 运单列表
				{ path: '/add_order', component: AddOrder },		// 添加运单
				{ path: '/customer_info', component: CustomerInfo },		// 客户信息
				{ path: '/add_customer', component: AddCustomer },		// 添加客户
				{ path: '/pandect_total', component: PandectTotal },		// 总览
				{ path: '/monitoring', component: Monitoring },		// 实时监控
				{ path: '/send_goods', component: SendGoods },		// 发货
				{ path: '/take_goods', component: TakeGoods },		// 收货
				{ path: '/route_ana', component: RouterAn },		// 线路分析
				{ path: '/temp_log', component: TemperLog },		// 温度详情
				{ path: '/order_content', component: OrderContent },		// 运单详情
			]
		},
		{		// 登录页面
			path: '/Login',
			name: 'Login',
			component: Login
		},
		{		// 开启项目，重定向到登录页面
			path: '/',
			redirect: '/Login'
		},
	],
	// 设置成history时，路由中的#号消失(默认为hash)
	mode: 'history' /*hash*/
})


// 路由拦截
router.beforeEach((to, from, next) => {
	// to 将要访问的路径
	// from 从哪个路径跳转而来
	// next 是一个函数，表示放行
	// next放行的两种方式：1、直接放行next()  2、跳转放行next('/login')
	// 判断是否登录
	const token = localStorage.getItem('token')
	// 如果访问login地址，则直接放行
	if (to.path === '/login') return next();
	// 给不访问权限页面的路由放行
	if (to.path === '/UpPwd') return next();
	// 如果访问有权限的网页，然后并没有登录，则跳转到登录页面
	if (!token) {
		return next('/login')
	}
	next()
})

export default router
